#pragma once

#include <cstdio>
#include <mc_runtime.h>

// MACA alignment is set to 16 to support the largest load type that we have =>
// uint4 (4*4 bytes) => 128 bit
#define MACA_ALIGNMENT 16

inline unsigned divUp(unsigned x, unsigned y) { return (x + y - 1) / y; }

#ifndef NDEBUG
#define MacaCheckError()
#else
#define MacaCheckError()                                                       \
  do {                                                                         \
    mcError_t err_ = mcGetLastError();                                       \
    if (err_ != mcSuccess) {                                                 \
      printf("MacaCheckError() failed at: %s:%d\n", __FILE__, __LINE__);       \
      printf("code: %d ; description: %s\n", err_, mcGetErrorString(err_));  \
      exit(1);                                                                 \
    }                                                                          \
                                                                               \
    err_ = mcDeviceSynchronize();                                            \
    if (mcSuccess != err_) {                                                 \
      printf("MacaCheckError() failed after sync at: %s:%d;\n", __FILE__,      \
             __LINE__);                                                        \
      printf("code: %d; description: %s\n", err_, mcGetErrorString(err_));   \
      exit(1);                                                                 \
    }                                                                          \
  } while (0)
#endif

#define MacaCheck(CALL)                                                        \
  do {                                                                         \
    if (mcSuccess != CALL) {                                                 \
      printf("MACA error at  %s:%d with error %s(%i)\n", __FILE__, __LINE__,   \
             mcGetErrorString(CALL), CALL);                                  \
      exit(CALL);                                                              \
    }                                                                          \
  } while (0);
